package com.cn.por.util.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JsonFileFinder {

    // 修改为你的文件夹路径
    private static String folderPath = "C:\\Users\\26983\\Desktop\\im\\images1";
    private static String endTxt = "C:\\Users\\26983\\Desktop\\im\\编程知识考核.txt";



    public static void main(String[] args) {
        File folder = new File(folderPath);
        List<JsonNode> resultJson = new ArrayList<>();
        if (folder.isDirectory()) {
            List<File> jsonFiles = getJsonFiles(folder);
            //读取json
            for (File jsonFile : jsonFiles) {
                ObjectMapper mapper = new ObjectMapper();
                mapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true);
                try {
                    JsonNode jsonNode = mapper.readTree(jsonFile);
                    Iterator<String> iterator = jsonNode.fieldNames();
                    while (iterator.hasNext()){
                        String next = iterator.next();
                        resultJson.add(jsonNode.get(next).get(0).get("text_lines"));
                    }
                    jsonFile = null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            System.out.println("The provided path is not a directory.");
        }

        /**
         * 读取每一个图片转json的对象，提取有用的信息到text文件
         */
        File txt  = new File(endTxt);
        String regex = "\\d*\\s*[．\\.]\\s*\\D*[？?]+";
        Pattern pattern = Pattern.compile(regex);
        String endText = "此解析是否帮助到您";
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(endTxt))){
            for (JsonNode jn: resultJson){
                boolean startFlag = false;
                for (JsonNode jsonNode: jn){
                    String itemText = (jsonNode.get("text")+"").replaceAll("^\"|\"$","");

                    /**
                     * 题目开始标识
                     */
                    if(!startFlag){
                        Matcher matcher = pattern.matcher(itemText);
                        while (matcher.find()){
                            startFlag = true;
                            //写入标题
                            writer.write(matcher.group()+"\n");
                        }
                    }else{
                        /**
                         * 题目结束标识
                         */
                        if(itemText.indexOf(endText)>=0){
                            break;
                        }
                        if(itemText.startsWith("多考解析")){
                            continue;
                        }
                        if(itemText.startsWith("掌握")){
                            continue;
                        }
                        if(itemText.startsWith("生疏")){
                            continue;
                        }
                        if(itemText.startsWith("易错")){
                            continue;
                        }
                        if(itemText.startsWith("简介 看懂")){
                            break;
                        }
                        if(itemText.startsWith("户 看不懂")){
                            break;
                        }
                        if(itemText.startsWith("推荐指数")){
                            continue;
                        }
                        if(itemText.startsWith("收藏")){
                            continue;
                        }
                        if(itemText.startsWith("参考解析")){
                            continue;
                        }
                        if(itemText.startsWith("会考解析")){
                            continue;
                        }
                        writer.write(itemText+"\n");
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static List<File> getJsonFiles(File folder) {
        List<File> jsonFiles = new ArrayList<>();
        File[] files = folder.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    jsonFiles.addAll(getJsonFiles(file)); // 递归调用
                } else if (file.isFile() && file.getName().endsWith(".json")) {
                    jsonFiles.add(file);
                }
            }
        }
        return jsonFiles;
    }
}
